const users = require("../../sql/users");
const sql = require('../../sql/mysql');
const jwt = require("jsonwebtoken");


module.exports = (req,res)=>{
    const un = req.fields.username;
    const pw = req.fields.password;
    sql.get(users,{username:un}).then(data=>{
        if(data.length>0){
            if(pw===data[0].password){
                sql.set(users,{username:un},{lasttime:Date.now()}).then(()=>{
                    const token = jwt.sign(
                        {username:un},
                        "大聪明",{
                            expiresIn:1000000
                        }
                    )
                    res.send({code:0,msg:"登录成功",roles:data[0].roles,token})
                })
                
            }else{
                res.send({code:1,msg:"密码错误"})
            }
        }else{
            res.send({code:2,msg:"不存在该用户名"})
        }
    })
}